package com.hoshiibuy.platform.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.platform.entity.Platform;
import com.hoshiibuy.platform.entity.param.PlatFormInsertParam;
import com.hoshiibuy.platform.entity.param.PlatFormParam;
import com.hoshiibuy.platform.entity.param.PlatFormUpdateParam;
import com.hoshiibuy.platform.service.PlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author yl
 * @title: PlaformController
 * @projectName hoshiibuy-platform
 * @description: 平台控制器
 * @date 2019/4/18 10:43
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/platform")
@Api(value = "平台管理", tags = {"平台操作接口"})
public class PlaformController {

    @Autowired
    private PlatformService platformService;

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 分页查询平台信息
     */
    @ApiOperation(value = "分页查询平台记录")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PlatFormParam param) {
        try {

            QueryWrapper<Platform> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(param.getPlatformName()))
                queryWrapper.lambda().eq(Platform::getPlatformName, param.getPlatformName());

            if (param.getCompanyid() != 0)
                queryWrapper.lambda().eq(Platform::getCompanyid, param.getCompanyid());

            if (StringUtils.isNotBlank(param.getLegalPersionName()))
                queryWrapper.lambda().eq(Platform::getLegalPersionName, param.getLegalPersionName());

            IPage<Platform> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

            return Response.data(platformService.page(page, queryWrapper));

        } catch (ServiceException e) {
            log.error("查询平台信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("分页查询平台信息异常", e);
            return Response.failed("分页查询平台信息异常");

        }

    }

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询平台信息
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询平台信息")
    public Response queryPlatformById(Long id) {
        return Response.data(platformService.getById(id));
    }

    /**
     * @param platformName
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/query/param")
    @ApiOperation(value = "查询平台信息")
    public Response queryPlatformByName(String platformName) {

        Platform platform;
        try {

            Wrapper wrapper = Wrappers.<Platform>query()
                    .lambda().eq(Platform::getPlatformName, platformName);
            platform = platformService.getOne(wrapper);

        } catch (ServiceException e) {

            log.error("查询平台信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("查询平台信息异常", e);
            return Response.failed("查询平台信息异常");
        }


        return Response.data(platform);

    }

    /**
     * @param platFormInsertParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台信息")
    public Response savePlatform(@RequestBody @Valid PlatFormInsertParam platFormInsertParam) {
        try {
            Platform platform = BeanUtils.covertType(platFormInsertParam, Platform.class);
            return Response.data(platformService.save(platform));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }

    }

    /**
     * @param platFormUpdateParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台信息")
    public Response updatePlatformById(@RequestBody @Valid PlatFormUpdateParam platFormUpdateParam) {
        try {
            Platform platform = BeanUtils.covertType(platFormUpdateParam, Platform.class);
            return Response.data(platformService.updateById(platform));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }

    }
}
